import {
	defineStore
} from "pinia"
import {
	nanoid
} from 'nanoid'
import overallRequest from "@/utool/http/http.js"
export default defineStore('trends', {
	state: () => ({
		page: 1,
		pages: 1,
		limit: 10,
		total: 1,
		list: [],
		foverTimeOut: null,
		PageCurrent: 0,
		Tabs: {
			show: false,
			current: 0,
			list: [{
					key: '1',
					value: '动态'
				},
				{
					key: '2',
					value: '附近'
				},
				{
					key: '3',
					value: '漂流瓶'
				}
			],
		},
		Trends: {
			initData: false,
			url: "/user/dynamicList",
			page: 1,
			pages: 1,
			limit: 10,
			total: 1,
			cityList: [],
			list: []
		},
		Nearby: {
			initData: false,
			url: "/user/GetNearbyList",
			page: 1,
			pages: 1,
			limit: 10,
			total: 1,
			list: []
		},
		Drift: {
			initData: false,
			url: "",
			page: 1,
			pages: 1,
			limit: 10,
			total: 1,
			list: []
		}
	}),
	getters: {
		GetList(state) {
			let page = state.page
			let limit = state.limit
			return state.list
		}
	},
	actions: {
		PageChangeUpdate: async function(index) {
			index = Number(index)
			this.Tabs.current = index
		},
		// 获取附近的人列表
		GetServerNearbyList: async function() {
			let url = this.Nearby.url
			let Querypage = this.Nearby.page
			let Querylimit = this.Nearby.limit
			let {
				data
			} = await overallRequest(url, {
				data: {
					page: Querypage,
					limit: Querylimit
				}
			})
			let {
				page,
				pages,
				limit,
				total,
				list
			} = data
			this.Nearby.initData = true
			this.Nearby.page = page
			this.Nearby.pages = pages
			this.Nearby.limit = limit
			this.Nearby.total = total
			this.Nearby.list.push(...list)
		},
		// 附近的人列表页码自增
		NearbyIncrPage: async function() {
			let Querypage = this.Nearby.page
			let Querypages = this.Nearby.pages
			if (Querypage >= Querypages) {
				return
			} else {
				this.Nearby.page++
				this.GetServerNearbyList()
			}
		},
		// 动态列表页码自增
		TrendsIncrPage: async function() {
			let Querypage = this.Trends.page
			let Querypages = this.Trends.pages
			if (Querypage >= Querypages) {
				return
			} else {
				this.Trends.page++
				this.GetServerTrendsList()
			}
		},
		// 获取动态列表
		GetServerTrendsList: async function() {
			let Querypage = this.Trends.page
			let Querylimit = this.Trends.limit
			let url = this.Trends.url
			let {
				data
			} = await overallRequest(url, {
				data: {
					page: Querypage,
					limit: Querylimit
				}
			})
			let {
				page,
				pages,
				limit,
				total,
				list
			} = data
			this.Trends.initData = true
			this.Trends.page = page
			this.Trends.pages = pages
			this.Trends.limit = limit
			this.Trends.total = total
			this.Trends.list.push(...list)
		},
		/**
		 * 点赞动态
		 */
		DynamicFover: async function(item) {
			let {
				id
			} = item
			let current_user_fover = item.current_user_fover
			if (current_user_fover) {
				item.current_user_fover = false
				if (item.fover <= 0) {
					item.fover = 0
				} else {
					item.fover--
				}
			} else {
				item.current_user_fover = true
				item.fover++
			}
			let url = "/user/FoverDynamic"
			await overallRequest(url, {
				method: "POST",
				data: {
					id
				}
			})
		},
		// 动态详细信息
		DynamicInfo: async function(data) {
			let url = "/user/dynamicInfo"
			return await overallRequest(url, {
				data
			})
		},
		// 动态评论列表
		DynamicReplyList: async function(data) {
			let url = "/user/dynamicCommentList"
			return await overallRequest(url, {
				data
			})
		},
		// 动态评论回复列表
		DynamicCommentReplyList: async function(data) {
			let url = "/user/GetDynamicCommentReplyList"
			return await overallRequest(url, {
				data
			})
		},
		// 动态评论点赞
		DynamicCommentFover: async function(data) {
			console.log("点击评论点赞");
		},
		// 动态评论删除
		DynamicCommentDelete: async function() {

		},
		// 回复评论
		CommentDynamic: async function(data) {
			let id = nanoid()
			data.id = id
			let url = "/user/CommentDynamic"
			return await overallRequest(url, {
				method: "POST",
				data
			})
		},
		// 动态点赞列表
		DynamicFoverList: async function(data) {
			let url = "/user/dynamicFoverList"
			return await overallRequest(url, {
				data
			})
		},
		// 删除评论
		DeleteComment: async function() {},
		// 删除动态
		DeleteDynamic: async function() {}
	}

})